import clr, sys

clr.AddReferenceByPartialName("Microsoft.Office.Interop.Outlook")
clr.AddReferenceByPartialName("office")

from Microsoft.Office.Interop import Outlook
from Microsoft import Office

from System.Reflection import *

from Globals import *

class GtdUi:
    def __init__(self, host, parent, attachOnActivate):
        self.host = host
        self.parent = parent
        self.attached = False
        self.gtdBar = None

    def OnClose(self):
        self.Detach()
        self.parent.remove(self)

    def OnActivate(self):
        if not self.attached:
            self.Attach()

    def Attach(self):
        if self.attached:
            return

        cmdbars = self.host.CommandBars

        for bar in cmdbars:
            if bar.Name.Equals("@GTD"):
                bar.Delete()

        self.gtdBar = cmdbars.Add("@GTD", \
                                  Office.Core.MsoBarPosition.msoBarTop, \
                                  False, \
                                  False)

        popup = self.gtdBar.Controls.Add(Office.Core.MsoControlType.msoControlPopup,\
                                         Missing.Value, \
                                         Missing.Value, \
                                         self.gtdBar.Controls.Count + 1, \
                                         1)
        popup.Caption = "@GTD"

        self.AddGtdControls()

        self.gtdBar.Enabled = True
        self.gtdBar.Visible = True

        self.attached = True

    def AddGtdControls(self):
        pass

    def AddCommandBarButton(self, caption, faceId, tooltipText, handler):
        button = self.gtdBar.Controls.Add(Office.Core.MsoControlType.msoControlButton, \
                                          Missing.Value, \
                                          Missing.Value, \
                                          self.gtdBar.Controls.Count + 1, \
                                          1)
        button.Style = Office.Core.MsoButtonStyle.msoButtonCaption
        button.Caption = caption
        button.TooltipText = tooltipText
        Office.Core._CommandBarButtonEvents_Event.add_Click(button, handler)
        
    def Detach(self):
        if not self.attached:
            return

        if self.gtdBar != None:
            gtdBar.Delete()

        self.attached = False
